package com.my.screenadapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by ${张英杰} on 2016/12/28.
 */

public class MainAdapter extends RecyclerView.Adapter {
    List<MainActivity.TempSave> mlist;

    public MainAdapter(List<MainActivity.TempSave> mlist) {
        this.mlist = mlist;
    }

    public void setData(List<MainActivity.TempSave> mlist) {
        this.mlist = mlist;
        notifyDataSetChanged();
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_mainadapter,
                viewGroup, false);
        return new MViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position, List payloads) {
        super.onBindViewHolder(holder, position, payloads);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        MViewHolder mViewHolder = (MViewHolder) holder;
        mViewHolder.itemView.setTag(position);
        mViewHolder.btn.setText(mlist.get(position).name);
        //手动更改高度，不同位置的高度有所不同
//        btn.setHeight(100 + (position % 3) * 100);
    }


    @Override
    public int getItemCount() {
        return mlist != null ? mlist.size() : 0;
    }

    public interface ItemClickListener {
        void itemClickListener(int position);
    }

    private ItemClickListener adapterInterface;

    public void setOnItemClickListener(ItemClickListener inter) {
        adapterInterface = inter;
    }


    class MViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        @BindView(R.id.btn)
        Button btn;
        private MViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
            btn.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            int position = (int)itemView.getTag();
            switch (v.getId()){
                case R.id.btn:
                    if (adapterInterface != null) {
                        adapterInterface.itemClickListener(position);
                    }
                    break;
            }
        }
    }
}
